package com.example.FileSearch;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by fany on 14-1-22.
 */
class SQLiteFileRecordAccessor extends SQLiteOpenHelper {

    public SQLiteFileRecordAccessor(Context context, String name, SQLiteDatabase.CursorFactory cursorFactory, int version)
    {
        super(context, name, cursorFactory, version);
    }
    
    
    @Override
    public void onCreate(SQLiteDatabase database) {
        database.execSQL(SqlDefinition_FileRecorder);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i2) {

    }

    public List<String> Search(String filter){
        List<String> results = new ArrayList<String>();
        SQLiteDatabase database = getReadableDatabase();
        try{
            String SQL = "select filename from IFileRecorder where filename like '%%%s%%' order by id limit 20 ";
            SQL = String.format(SQL, filter);
            Cursor cursor = database.rawQuery(SQL, null);
            while (cursor.moveToNext()) {
                results.add(cursor.getString(0));
            }
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
        finally {
            database.close();
        }
        return results;
    }
    public boolean HasLoaded() {
        SQLiteDatabase database = getReadableDatabase();
        int recordCount = 0;
        try{
            final String SQL = "select id from IFileRecorder";
            Cursor cursor = database.rawQuery(SQL,new String[]{});
            recordCount = cursor.getCount();
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
        finally {
            database.close();
        }
        return  recordCount > 0;
    }

    public void Clear() {
        SQLiteDatabase database = getWritableDatabase();
        try{
        final String SQL = "delete from IFileRecorder";
        database.execSQL(SQL);
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
        finally {
            database.close();
        }
    }

    public void Locate(String filename) {
//        SQLiteDatabase db = getReadableDatabase();
//        final String SQL = "select filename from IFileRecorder where filename like %?%";
//        Cursor cursor = db.rawQuery(SQL,new String[] {filter});
//        List<String> results = new ArrayList<String>();
//        while (cursor.moveToNext()) {
//            results.add(cursor.getString(0));
//        }
//        db.close();
//        return results;
    }

    public void Insert(String filename) {
        SQLiteDatabase database = getWritableDatabase();
        try{
            final String SQL = "insert into IFileRecorder (filename) values('%s')";
            database.execSQL(String.format(SQL, filename));
        }catch(Exception ex){
            ex.printStackTrace();
        }
        finally {
            database.close();
        }
    }

    public void Delete(String filename) {
        SQLiteDatabase database = getWritableDatabase();
        try{
            final String SQL = "delete from IFileRecorder";
            database.execSQL(SQL);
        }catch(Exception ex){
            ex.printStackTrace();
        }
        finally {
            database.close();
        }
    }

    private static final String SqlDefinition_FileRecorder = "create table IFileRecorder(id integer primary key autoincrement,filename varchar(255))";
    private static final String TableName = "IFileRecorder";
}
